<?php
require_once('TVB/pms/pdes/action/InfoGetAction.class.php');

class InfoRecipeUpdateAction extends BaseAction 
{
	public $id;
	/**
	 * @maxLength 255
	 * @label Dish Name (Chinese)
	 */
	public $dish_name_chinese;
	/**
	 * @maxLength 255
	 * @label Dish Name (English)
	 */
	public $dish_name_english;
	public $cuisine_category;
	public $cuisine_sub_category;
	public $food_category;
	public $method_type;
	public $main_ingredients;
	public $ingredients;
	public $seasonings;
	public $sauce;
	public $cooking_method;
	public $hints;
	public $designed_by;
	public $presented_by;
	public $purpose;
	public $useful_information;
	
	/**
	 * @filter datetime
	 * @label Publish Date/Time
	 */
    public $publish_datetime;
	
	/**
	 * @filter datetime
	 * @label Expire Date/Time
	 */
    public $expire_datetime;
	
	public $status;
	public $created_by;
	public $created_at;
	public $modified_by;
	public $modified_at;
	public $episode_id;

	public $programme_id;
	public $onair_episode_no;

	#public $role_id;
	public $cast_id; # modify by vanni,for bug 7397

	public $name_type;
	public $name_last;
	public $name_first;
	public $alphabet;
	public $description;

	public $ut;
	public $reset;
	public $from;
	public function execute(){
		if (!is_null($this->reset))
		{
			$this->setData('id', $this->id);
			$this->setData('type', 'recipe');
			$this->setData('act', 'edit');
			$this->setData('from', $this->from);
			return 'reset';
		}

		Model::$dbo['w']->beginTransaction();
		try
		{
			if (CommonUtils::is_concurrent('info_recipe', $this->id, $this->modified_at))
			{
				$this->init_page_data();
				$this->addError('concurrent', array('0105', 'Concurrent update found!'));
				return 'logic-error';
			}

			if(count($this->_errors) > 0){
				$this->init_page_data();
				return 'filter-error';
			}

			//check name unique
			if (is_null($this->dish_name_chinese) && is_null($this->dish_name_english))
			{
				$this->init_page_data();
				$this->addError('Recipe.Dish Name', array('0039', 'Dish Name'));
				return 'filter-error';
			}

            //check publish_datetime && expire_datetime
            if (empty($this->publish_datetime)
                || empty($this->expire_datetime)
                || $this->publish_datetime > $this->expire_datetime) {
                
                $this->init_page_data();
                $this->addError('Publish Date/Time && Expire Date/Time', array('0033', 'Publish Date/Time && Expire Date/Time'));
                return 'filter-error';
            }

			$update_id = $this->update_info_recipe();
			if ($update_id >= 0 )
			{
				$this->setData('type', 'recipe');
				$this->setData('onair_episode_no', $this->onair_episode_no);
				$this->setData('programme_id', $this->programme_id);
			}

			Model::$dbo['w']->commit();

			return ($this->from == 'task') ? "task" : "succ";
		}
		catch(Exception $e)
		{
			error_log("Update Recipe Fails: {$this->id}" . var_export($this, 1));
			error_log("Exception: " . var_export($e, 1));

			Model::$dbo['w']->rollBack();

			return 'system-error';
		}
	}    

	/**
	 * add info_recipe information
	 */
	private function update_info_recipe()
	{
		
		$info_recipe = new info_recipe();
		$info_recipe = $info_recipe->get($this->id);
		Model::diff($info_recipe, $this);
		$info_recipe->modified_at = date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']);
		$info_recipe->modified_by = CommonUtils::get_user();
		$info_recipe->designed_by = CommonUtils::json_info_data($this, 'designed_by');
		$info_recipe->presented_by = CommonUtils::json_info_data($this, 'presented_by');
		$info_recipe->dish_name_chinese = trim($this->dish_name_chinese);
		$info_recipe->dish_name_english = trim($this->dish_name_english);
		$info_recipe->description = trim($this->description);
        $info_recipe->publish_datetime = $this->publish_datetime;
        $info_recipe->expire_datetime = $this->expire_datetime; 

		$update_id = Model::$dbo['w']->save($info_recipe);
		if ($update_id > 0)
		{
			$info_recipe->add_change_log();

            $primary_key = array(
                'id'    => $this->id
            );
            $fields = array(
                'dish_name_chinese',
                'dish_name_english',
                'ingredients',
                'seasonings',
                'sauce',
                'cooking_method',
                'hints',
                'designed_by',
                'presented_by',
                'purpose',
                'useful_information',
                'description'
            );
            CommonUtils::filterC2A0('info_recipe', $primary_key, $fields);
			$this->addInfo("info_recipe:".AC::LOG_OP_UPDATE.":".$this->id, "update info_recipe success", json_encode($info_recipe));
			return $update_id;
		}
		else
		{
			$this->addError("info_recipe:".AC::LOG_OP_UPDATE.":".$this->id, "update info_recipe error", json_encode($info_recipe));
			return false;
		}

	}

	/**
	 * init page data
	 */
	private function init_page_data()
	{
		$action = new InfoGetAction();
		$action->id = $this->id;
		$action->act = 'edit';
		$action->type = 'recipe';
		$action->execute();

		$old_data = $action->getData();
		$this->modified_at = $old_data['recipe']->modified_at;
		$old_data['recipe'] = Model::diff($old_data['recipe'], $this);
		foreach ($old_data as $object=>$data)
		{
			$this->setData($object, $data);
		}

		$json_fields = AC::$infoJson['info_recipe'];
		$json_data = array();
		foreach ($json_fields as $field)
		{
			$json_decode_data = json_decode(CommonUtils::json_info_data($this, $field), true);
			$json_data[$field] = isset($json_decode_data[$field]) ? $json_decode_data[$field] : array();
		}

		$this->setData("recipe_json_list", $json_data);
		//jump page
		$this->setData('id', $this->id);
		$this->setData('type', 'recipe');
		$this->setData('act', 'edit');
		$this->setData('from', $this->from);
	}
}
?>
